package com.mashibing.conf;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * @author liuyno
 * @date 2021/3/7 - 23:10
 */
public class ZKUtil {

    private static ZooKeeper zk;
//    private static String url = "192.168.1.10:2181,192.168.1.20:2181,192.168.1.30:2181/testConf";
    private static String url = "192.168.1.10:2181,192.168.1.20:2181,192.168.1.30:2181/testLock";
    private static DefaultWatcher watcher = new DefaultWatcher();
    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static ZooKeeper getZK() {
        try {
            zk = new ZooKeeper(url, 3000, watcher);
            watcher.setCd(countDownLatch);
            countDownLatch.await();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return zk;
    }

}
